package com.easybuilder.core.rbac.service.impl;

import com.easybuilder.core.rbac.dao.SysDeptDao;
import com.easybuilder.core.rbac.dto.SysDeptDTO;
import com.easybuilder.core.rbac.entity.SysDept;
import com.easybuilder.core.rbac.service.SysDeptService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 *
 * @author 张兴帅
 * @projectName easy-builder
 * @packageNmae com.easybuilder.core.rbac.service.impl
 * @fileNmae SysDeptServiceImpl
 * @date 2023-5-16
 * @copyright 华能信息技术有限公司
 * @since 0.0.1
 */

@Service
public class SysDeptServiceImpl implements SysDeptService {
    @Resource
    private SysDeptDao deptDao;

    @Override
    public void save(SysDept sysDept) {
        if (sysDept.getId() == null) {
            deptDao.insertSelective(sysDept);
        } else {
            deptDao.updateByPrimaryKeySelective(sysDept);
        }
    }

    @Override
    public List<SysDeptDTO> loadByPid(Long pid) {
        List<SysDept> list = deptDao.getByParentId(pid);
        List<SysDeptDTO> result = new ArrayList<>(list.size());
        // 设置是否存在子节点的标识
        for (SysDept sysDept : list) {
            SysDept dept = new SysDept();
            dept.setParentId(sysDept.getId());
            int count = deptDao.selectCount(dept);
            SysDeptDTO sysDeptDTO = new SysDeptDTO();
            BeanUtils.copyProperties(sysDept, sysDeptDTO);
            sysDeptDTO.setHasChildren(count>0);
            result.add(sysDeptDTO);
        }
        return result;
    }
}